﻿using Kingdee.BOS;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.List;
using Kingdee.BOS.ServiceHelper;
using Kingdee.BOS.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DevSamPle.FormPlugin
{
    [Description("【表单插件】打开基础资料列表，给文本字段赋值"), HotUpdate]
    public class OpenListSetValue : AbstractDynamicFormPlugIn
    {
        /// <summary>
        /// 打开基础资料列表，把多语言数据复制给多语言字段（因为多语言字段无法显示编码按钮，只好用一个普通按钮代替）
        /// </summary>
        /// <param name="e"></param>
        public override void ButtonClick(ButtonClickEventArgs e)
        {
            base.ButtonClick(e);

            string formId = "BD_Supplier";
            var listShowParameter = new ListShowParameter();
            listShowParameter.PageId = Guid.NewGuid().ToString();
            listShowParameter.FormId = formId;
            listShowParameter.MultiSelect = false;
            listShowParameter.IsLookUp = true;
            this.View.ShowForm(listShowParameter, result =>
            {
                if (result == null)
                {
                    return;
                }
                var returnData = result.ReturnData as ListSelectedRowCollection;
                if (returnData == null || returnData.Count == 0)
                {
                    return;
                }
                var rowData = returnData.FirstOrDefault();
                //只要当前语言的名称
                this.Model.SetValue("F_POSW_MulLangText", new LocaleValue(rowData.Name));
                //要所有语言的名称
                var supplierBusinessInfo = FormMetaDataCache.GetCachedFormMetaData(this.Context, formId).BusinessInfo;
                var subDynamicObjectType = supplierBusinessInfo.GetSubBusinessInfo(new List<string>(new[] { "FName" })).GetDynamicObjectType();
                var obj = BusinessDataServiceHelper.LoadSingle(this.Context, rowData.PrimaryKeyValue, subDynamicObjectType);
                this.Model.SetValue("F_POSW_MulLangText", obj["Name"]);
            });
        }
        /// <summary>
        /// 打开基础资料列表，把文本复制给文本字段,
        /// 会把原来的逻辑复盖掉，只生效自定义的效果
        /// </summary>
        /// <param name="e"></param>
        public override void BeforeF7Select(BeforeF7SelectEventArgs e)
        {
            string formId = "BD_Supplier";
            if (e.FieldKey.EqualsIgnoreCase("FSupplierId"))
            {
                var listShowParameter = (ListShowParameter)e.DynamicFormShowParameter;
                listShowParameter.PageId = Guid.NewGuid().ToString();
                listShowParameter.FormId = formId;
                listShowParameter.MultiSelect = false;
                listShowParameter.IsLookUp = true;
                this.View.ShowForm(listShowParameter, result =>
                {
                    if (result == null) { return; }
                    var returnData = result.ReturnData as ListSelectedRowCollection;
                    if (returnData == null || returnData.Count == 0)
                    {
                        return;
                    }
                    var rowData = returnData.FirstOrDefault();
                    this.Model.SetValue("F_POSW_Text", rowData.Number);
                });
            }
            base.BeforeF7Select(e);
        }
    }
}
